#include "AppVPFinder.h"
#include "abliefern/VanishingPointOracle.h"

//#define DO_BLUR




#define MAX_DISTANCE 5
const int THRESHOLD_SQUARED = 225;
const int THRESHOLD = 15;

#include <sqlite3.h>





using namespace abliefern;


bool compareVP(VanishingPoint* the1, VanishingPoint* the2){
	return (the1->supportCount > the2->supportCount);
}


//--------------------------------------------------------------
void AppVPFinder::setup(){
	
	
	/* List dir */
	DIR.setVerbose(false);
	DIR.allowExt("png");
    nImages = DIR.listDir("input");
	
	
	/* DB */
	
	_myDatabaser.openDB();

	
	
	printf("%d images found\n", nImages);
	
	_myImageCounter = 0;
	_myEnterCount = 0;

	_myOracle = NULL; // this is important! not always null initialized
	
	_myWeAreDone = false;
	
//	ofEnableSmoothing();
	
	
	std::vector<int> myIgnores;
	std::vector<ImageSegment>* mySegments = _myDatabaser.retrieveGoodSegements(321, 4451, 200, 400000, 2000000, &myIgnores);
	
	
	
	if(mySegments == NULL){
		std::cout << " I is NULL \n";

	} else{
		std::cout << " I can has " << mySegments->size() << "\n";
		

}
	
	
	
}

//--------------------------------------------------------------
void AppVPFinder::update(){
	
	
	
	
	if(_myOracle == NULL && _myImageCounter < nImages && !_myWeAreDone){
		
		printf("### NEW \n");

		_myOracle = new VanishingPointOracle(DIR.getName(_myImageCounter), DIR.getPath(_myImageCounter));
		

		_myOracle->start();
	} else if(_myOracle != NULL && !_myWeAreDone) {
		
		if(_myOracle != NULL) _myOracle->update();
		
		if(_myOracle->hasFinishedCompletely() ){
			
			KlausImage* myImage = new KlausImage(_myOracle->getFileName(), _myOracle->getWidth() * 2, _myOracle->getHeight() * 2);
			
			int myImageId = _myDatabaser.saveImage(myImage);
			
			_myDatabaser.setImageState(myImage, 1);
			
			std::vector<VanishingPoint> myVPs;
			myVPs = _myOracle->getVanishingPoints();
			
			for(int i = 0; i < myVPs.size(); i++){
				VanishingPoint* myVP = &myVPs.at(i);
				
				myVP->x = myVP->x * 2;
				myVP->y = myVP->y * 2;
				
				_myDatabaser.saveVanishingPoint(myImage, myVP);
				
			}
			
			
			delete myImage;
			
			delete _myOracle;
			
			_myOracle = NULL;
					
			_myEnterCount = 0;
			
			_myImageCounter++;
			
			if(_myImageCounter >= nImages){
				_myWeAreDone = true;
				
				printf("### We are done \n");
				
				
				_myDatabaser.closeDB();
			}
			
		
			
		} else {
			// just keep on going
		}
	}  
	
	
	
	  
}


//--------------------------------------------------------------
void AppVPFinder::draw(){

	float myScale = 0.2;
	
	float myXOffset = 3400;
	float myYOffset = 1500;
	
	if(_myEnterCount > 0){
		ofBackground(60, 0, 0);

	} else {
		ofBackground(0, 0, 0);

	}
	
	
	if(_myOracle == NULL || _myWeAreDone){
		
		ofBackground(255, 255, 0);
	}
	
	
	
	if(_myOracle != NULL){
		_myOracle->draw();
	}
	
		
		
				
		
		/* Create openframeworks image for saving */
		//ofImage* mySaveImage = new ofImage;
		//mySaveImage->setFromPixels((unsigned char*)myFullImage->imageData, myFullImage->width, myFullImage->height, OF_IMAGE_COLOR_ALPHA, TRUE);
		
		
		//mySaveImage->saveImage("foomanchu.png");
		
		
		/* Drawing stuff */
				
				
		
		
	
	
		
	
}





//--------------------------------------------------------------
void AppVPFinder::keyPressed  (int key){

	
	if(_myOracle != NULL && _myOracle->hasFinishedLoading()){
		switch (key) {
			case 'r': // reset. do it again
				_myEnterCount = 0;
				
				delete _myOracle;
				_myOracle = NULL;
				
				break;
				
			case OF_KEY_RETURN: // submit 
				_myEnterCount++;
				
				printf("Enter pressed %d\n", _myEnterCount);
				
				if(_myEnterCount > 1){
					_myOracle->finishDetection();
					
					
				}
				break;
				
			case 'y': // accept
				_myEnterCount = 0;
				_myOracle->acceptVanishingPoint();
				break;
				
				
			case 'n': // deny
				_myEnterCount = 0;
				_myOracle->denyVanishingPoint();
				break;
				
				
			case 'i': // kill the lines
				_myEnterCount = 0;
				_myOracle->ignoreVanishingPoint();
				break;
				
				
			case '+': // zoom
				_myOracle->zoomIn();
				break;
				
			case '-': // zoom
				_myOracle->zoomOut();
				break;
				
				
			default:
				_myEnterCount = 0;
				break;
		}
	}
}

//--------------------------------------------------------------
void AppVPFinder::keyReleased(int key){
	
}

//--------------------------------------------------------------
void AppVPFinder::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void AppVPFinder::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void AppVPFinder::mousePressed(int x, int y, int button){
	
}

//--------------------------------------------------------------
void AppVPFinder::mouseReleased(int x, int y, int button){
	
}

//--------------------------------------------------------------
void AppVPFinder::resized(int w, int h){
	
}








	




